package com.efruit.poadapters;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.efruit.authority.IUserInfo;
import com.efruit.authority.IUserInfoFetcher;
import com.efruit.dao.IUserDAO;
import com.efruit.po.User;

public class UserInfoFetcherImpl implements IUserInfoFetcher {
	private static int count = 0;
	private int mycount = 0;

	private IUserDAO userdao = null;
	private User user;

	public UserInfoFetcherImpl() {
		mycount = count++;
		System.out.println("constructor " + (mycount));
	}

	public IUserDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(IUserDAO userdao) {
		System.out.print("set user dao " + mycount);
		System.out.println(userdao == null ? "null" : "not null");
		this.userdao = userdao;
	}

	@Override
	public IUserInfo getUserInfo(String username, String password) {
		List userList = userdao.findByUsernamePassword(username, password);
		if (userList.size() > 0) {
			user = (User) userList.get(0);
			ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
					"applicationContext.xml");
			IUserInfo userInfo = (IUserInfo) ctx.getBean("UserInfoImpl");
			userInfo.setUserId(user.getUserId());
			userInfo.setUsername(user.getName());
			userInfo.setPassword(user.getPassword());
			userInfo.setAuthority(user.getAuthority());
			return userInfo;
		}
		return null;
	}

}
